<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="隐患描述" prop="yhRepresent">
        <el-input v-model="queryParams.yhRepresent" placeholder="请输入隐患描述" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="隐患等级" prop="yhLevel">
        <!-- ( 1A级重大隐患  2B级隐患  3C级隐患  4一级隐患) -->
        <el-select v-model="queryParams.yhLevel" placeholder="请选择隐患等级" clearable size="small">
          <el-option v-for="dict in yhLevelOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患类型" prop="yhType">
        <!-- ( 1事故隐患  2重大事故隐患  3轻微隐患) -->
        <el-select v-model="queryParams.yhType" placeholder="请选择隐患类型" clearable size="small">
          <el-option v-for="dict in yhTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患状态" prop="yhStatus">
        <!-- ( 1待整改  2整改中  3已闭环 4待复查) -->
        <el-select v-model="queryParams.yhStatus" placeholder="请选择隐患状态" clearable size="small">
          <el-option v-for="dict in yhStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item label="发现时间" prop="yhCreateTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.yhCreateTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择隐患发现时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['anquan:yh:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['anquan:yh:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['anquan:yh:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['anquan:yh:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="yhList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="隐患描述" align="center" prop="yhRepresent" />
      <el-table-column label="隐患等级" align="center" prop="yhLevel" :formatter="yhLevelFormat" />
      <el-table-column label="整改人" align="center" prop="yhEditMan" />
      <el-table-column label="整改类型" align="center" prop="yhEditType" :formatter="yhEditTypeFormat" />
      <el-table-column label="隐患发现时间" align="center" prop="yhCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.yhCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改限期" align="center" prop="yhEditDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.yhEditDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" align="center" prop="yhStatus" :formatter="yhStatusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['anquan:yh:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['anquan:yh:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改隐患对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="责任部门" prop="yhApartment">
          <el-input v-model="form.yhApartment" placeholder="请输入责任部门" />
        </el-form-item>
        <el-form-item label="隐患等级( 1A级重大隐患  2B级隐患  3C级隐患  4一级隐患)">
          <el-select v-model="form.yhLevel" placeholder="请选择隐患等级( 1A级重大隐患  2B级隐患  3C级隐患  4一级隐患)">
            <el-option v-for="dict in yhLevelOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隐患类型( 1事故隐患  2重大事故隐患  3轻微隐患)">
          <el-select v-model="form.yhType" placeholder="请选择隐患类型( 1事故隐患  2重大事故隐患  3轻微隐患)">
            <el-option v-for="dict in yhTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隐患状态( 1待整改  2整改中  3已闭环 4待复查)">
          <el-select v-model="form.yhStatus" placeholder="请选择隐患状态( 1待整改  2整改中  3已闭环 4待复查)">
            <el-option v-for="dict in yhStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隐患整改类型">
          <el-select v-model="form.yhEditType" placeholder="请选择隐患整改类型">
            <el-option v-for="dict in yhEditTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隐患整改情况" prop="yhEditSituation">
          <el-input v-model="form.yhEditSituation" placeholder="请输入隐患整改情况" />
        </el-form-item>
        <el-form-item label="整改人" prop="yhEditMan">
          <el-input v-model="form.yhEditMan" placeholder="请输入整改人" />
        </el-form-item>
        <el-form-item label="责任人" prop="yhMan">
          <el-input v-model="form.yhMan" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="所属风险点" prop="yhRiskPoint">
          <el-input v-model="form.yhRiskPoint" placeholder="请输入所属风险点" />
        </el-form-item>
        <el-form-item label="隐患上报" prop="yhReport">
          <el-input v-model="form.yhReport" placeholder="请输入隐患上报" />
        </el-form-item>
        <el-form-item label="附件" prop="yhEnclosure">
          <el-input v-model="form.yhEnclosure" placeholder="请输入附件" />
        </el-form-item>
        <el-form-item label="整改措施" prop="yhEditMeasures">
          <el-input v-model="form.yhEditMeasures" placeholder="请输入整改措施" />
        </el-form-item>
        <el-form-item label="整改要求" prop="yhEditAsk">
          <el-input v-model="form.yhEditAsk" placeholder="请输入整改要求" />
        </el-form-item>
        <el-form-item label="整改限期" prop="yhEditDate">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.yhEditDate" type="date"
            value-format="yyyy-MM-dd" placeholder="选择整改限期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="隐患发现时间" prop="yhCreateTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.yhCreateTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择隐患发现时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="隐患描述" prop="yhRepresent">
          <el-input v-model="form.yhRepresent" placeholder="请输入隐患描述" />
        </el-form-item>
        <el-form-item label="整改附件" prop="yhEditEnclosure">
          <el-input v-model="form.yhEditEnclosure" placeholder="请输入整改附件" />
        </el-form-item>
        <el-form-item label="复查人" prop="yhCheckMan">
          <el-input v-model="form.yhCheckMan" placeholder="请输入复查人" />
        </el-form-item>
        <el-form-item label="复查期限" prop="yhCheckDate">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.yhCheckDate" type="date"
            value-format="yyyy-MM-dd" placeholder="选择复查期限">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="复查期限" prop="other">
          <el-input v-model="form.other" placeholder="请输入复查期限" />
        </el-form-item>
        <el-form-item label="复查期限" prop="other1">
          <el-input v-model="form.other1" placeholder="请输入复查期限" />
        </el-form-item>
        <el-form-item label="复查期限" prop="other2">
          <el-input v-model="form.other2" placeholder="请输入复查期限" />
        </el-form-item>
        <el-form-item label="复查期限" prop="other3">
          <el-input v-model="form.other3" placeholder="请输入复查期限" />
        </el-form-item>
        <el-form-item label="复查期限" prop="other4">
          <el-input v-model="form.other4" placeholder="请输入复查期限" />
        </el-form-item>
        <el-form-item label="复查期限" prop="other5">
          <el-input v-model="form.other5" placeholder="请输入复查期限" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listYh, getYh, delYh, addYh, updateYh, exportYh } from "@/api/anquan/sktx/yhpcyzl/yhzg";

  export default {
    name: "Yh",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 隐患表格数据
        yhList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 隐患等级( 1A级重大隐患  2B级隐患  3C级隐患  4一级隐患)字典
        yhLevelOptions: [],
        // 隐患类型( 1事故隐患  2重大事故隐患  3轻微隐患)字典
        yhTypeOptions: [],
        // 隐患状态( 1待整改  2整改中  3已闭环 4待复查)字典
        yhStatusOptions: [],
        // 隐患整改类型字典
        yhEditTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          yhApartment: undefined,
          yhLevel: undefined,
          yhType: undefined,
          yhStatus: undefined,
          yhEditType: undefined,
          yhEditSituation: undefined,
          yhEditMan: undefined,
          yhMan: undefined,
          yhRiskPoint: undefined,
          yhReport: undefined,
          yhEnclosure: undefined,
          yhEditMeasures: undefined,
          yhEditAsk: undefined,
          yhEditDate: undefined,
          yhCreateTime: undefined,
          yhRepresent: undefined,
          yhEditEnclosure: undefined,
          yhCheckMan: undefined,
          yhCheckDate: undefined,
          other: undefined,
          other1: undefined,
          other2: undefined,
          other3: undefined,
          other4: undefined,
          other5: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("s_hiddenDanger_level").then(response => {
        this.yhLevelOptions = response.data;
      });
      this.getDicts("s_hiddenDanger_type").then(response => {
        this.yhTypeOptions = response.data;
      });
      this.getDicts("s_hiddenDanger_status").then(response => {
        this.yhStatusOptions = response.data;
      });
      this.getDicts("s_hiddenDanger_edit_tpe").then(response => {
        this.yhEditTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询隐患列表 */
      getList() {
        this.loading = true;
        listYh(this.queryParams).then(response => {
          this.yhList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 隐患等级( 1A级重大隐患  2B级隐患  3C级隐患  4一级隐患)字典翻译
      yhLevelFormat(row, column) {
        return this.selectDictLabel(this.yhLevelOptions, row.yhLevel);
      },
      // 隐患类型( 1事故隐患  2重大事故隐患  3轻微隐患)字典翻译
      yhTypeFormat(row, column) {
        return this.selectDictLabel(this.yhTypeOptions, row.yhType);
      },
      // 隐患状态( 1待整改  2整改中  3已闭环 4待复查)字典翻译
      yhStatusFormat(row, column) {
        return this.selectDictLabel(this.yhStatusOptions, row.yhStatus);
      },
      // 隐患整改类型字典翻译
      yhEditTypeFormat(row, column) {
        return this.selectDictLabel(this.yhEditTypeOptions, row.yhEditType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          yhId: undefined,
          yhApartment: undefined,
          yhLevel: undefined,
          yhType: undefined,
          yhStatus: undefined,
          yhEditType: undefined,
          yhEditSituation: undefined,
          yhEditMan: undefined,
          yhMan: undefined,
          yhRiskPoint: undefined,
          yhReport: undefined,
          yhEnclosure: undefined,
          yhEditMeasures: undefined,
          yhEditAsk: undefined,
          yhEditDate: undefined,
          yhCreateTime: undefined,
          yhRepresent: undefined,
          yhEditEnclosure: undefined,
          yhCheckMan: undefined,
          yhCheckDate: undefined,
          createTime: undefined,
          updateTime: undefined,
          other: undefined,
          other1: undefined,
          other2: undefined,
          other3: undefined,
          other4: undefined,
          other5: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.yhId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加隐患";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const yhId = row.yhId || this.ids
        getYh(yhId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改隐患";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.yhId != undefined) {
              updateYh(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addYh(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const yhIds = row.yhId || this.ids;
        this.$confirm('是否确认删除隐患编号为"' + yhIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delYh(yhIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () { });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有隐患数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportYh(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () { });
      }
    }
  };
</script>